$(function() {
	// 查询
	$(".g-search").click(function() {
		var modelId = $("#search-model").val();
		var datagrid = $('#model-action-infos').datagrid('options');
		if (modelId < 1) {
			datagrid.url = '/backend/authorize/list/model/action/infos';
		} else {
			datagrid.url = '/backend/authorize/list/model/action/infosbymodelid/' + modelId;
		}
		$('#model-action-infos').datagrid('reload');
	});
	$('#model-infos').datagrid({
		fitColumns : true,
		scrollbarSize : 0,
		singleSelect : true,
		data : modelData,
		columns : [ [ {
			field : 'id',
			title : 'ID',
			width : 20
		}, {
			field : 'name',
			title : '模块名称',
			width : 30
		}, {
			field : 'keyword',
			title : '关键字',
			width : 30
		}, {
			field : 'createAt',
			title : '创建时间',
			width : 20,
			formatter : function(value, row, index) {
				return moment(value).format('YYYY-MM-DD HH:mm:ss');
			}
		} ] ]
	});
	$('#model-action-infos').datagrid({
		fitColumns : true,
		width : 'auto',
		scrollbarSize : 0,
		url : '/backend/authorize/list/model/action/infos',
		method : 'GET',
		singleSelect : true,
		toolbar : '#model-action-toolbar',
		columns : [ [ {
			field : 'id',
			title : 'ID',
			width : fixWidth(0.04)
		}, {
			field : 'modelId',
			title : '模块',
			width : fixWidth(0.08),
			formatter : function(value, row, index) {
				if (modelData) {
					for (var i = 0; i < modelData.length; i++) {
						if (parseInt(modelData[i].id) === parseInt(value)) {
							return modelData[i].name;
						}
					}
				}
				return value
			}
		}, {
			field : 'name',
			title : '动作名称',
			width : fixWidth(0.10)
		}, {
			field : 'type',
			title : '动作类型',
			width : fixWidth(0.08),
			formatter : function(value, row) {
				if (isNaN(parseInt(row.id))) {
					return '';
				}
				if (parseInt(value) === 1) {
					return '模块列表';
				} else if (parseInt(value) === 2) {
					return 'Ajax请求';
				} else {
					return '未定义'
				}
			}
		}, {
			field : 'keyword',
			title : 'Uri',
			width : fixWidth(0.20)
		}, {
			field : 'orderNum',
			title : '排序',
			width : fixWidth(0.04)
		}, {
			field : 'remark',
			title : '备注',
			width : fixWidth(0.20)
		}, {
			field : 'createAt',
			title : '创建时间',
			width : fixWidth(0.14),
			formatter : function(value, row, index) {
				if (!value) {
					return ''
				}
				return moment(value).format('YYYY-MM-DD HH:mm:ss');
			}
		}, {
			field : 'operation',
			title : '操作',
			width : fixWidth(0.08),
			formatter : function(value, row, index) {
				if (isNaN(parseInt(row.id))) {
					return '';
				}
				var html = '<span class="l-btn-icon icon-edit" onclick="actionedit(';
				html += row.id;
				html += ')" style="position: static; cursor: pointer;">&nbsp;</span>';
				html += '&nbsp;&nbsp; &nbsp;&nbsp;<span class="l-btn-icon icon-no"  onclick="actiondelet(';
				html += row.id;
				html += ')" style="position: static; cursor: pointer;">&nbsp;</span>';
				return html;
			}
		} ] ],
		onLoadSuccess : function(data) {
			if (data.total == 0) {
				$('#model-action-infos').datagrid('appendRow', {
					id : '无记录'
				});
			}
		}
	});

	$(window).on("resize", function() {
		$('#model-infos').datagrid('resize', {
			width : datagridResizeWidth()
		});
		$('#model-action-infos').datagrid('resize', {
			width : datagridResizeWidth()
		});
	});
})
var html = $("#append-form").html();
// 方法
var appendindex;
function append() {
	// 页面层
	layer.ready(function() {
		appendindex = layer.open({
			type : 1,
			title : '新增动作',
			skin : 'layui-layer-rim', // 加上边框
			area : [ '500px', '460px' ], // 宽高
			btn : [ '确定保存', '取消' ],
			content : html,
			success : function(layero, index) {
				var datas = $("#model-action-infos").datagrid('getData');
				var orderNum = 0;
				for (var i = 0; i < datas.rows.length; i++) {
					if (orderNum < datas.rows[i].orderNum) {
						orderNum = datas.rows[i].orderNum;
					}
				}
				$(".layui-layer-content input[name='modelOrder']").val(orderNum + 1);
			},
			btn1 : function(index, layero) {
				$(".layui-layer-content").mLoading("show");
				var id = $(".layui-layer-content select[name='modelId']").val();
				var name = $.trim($(".layui-layer-content input[name='modelName']").val());
				var keyword = $.trim($(".layui-layer-content input[name='modelKeyword']").val());
				var type = $(".layui-layer-content select[name='actionType']").val();
				var modelOrder = $(".layui-layer-content input[name='modelOrder']").val();
				if (isNaN(parseInt(modelOrder))) {
					modelOrder = 1;
				} else {
					modelOrder = parseInt(modelOrder);
				}
				var remark = $.trim($(".layui-layer-content textarea[name='remark']").val());
				if (name === '') {
					layer.msg('动作名称不能为空！', {
						icon : 2,
						time : 2000
					});
					return;
				}
				if (keyword === '') {
					layer.msg('动作关键字不能为空！', {
						icon : 2,
						time : 2000
					});
					return;
				}

				request.post('/backend/authorize/list/model/action/insert', {
					modelId : id,
					name : name,
					keyword : keyword,
					type : type,
					orderNum : modelOrder,
					remark : remark
				}, function(response, data) {
					if (data && data.id && parseInt(data.id) > 0) {
						$('#model-action-infos').datagrid('reload');
					}
				}, null, function() {
					$(".layui-layer-content").mLoading("hide");
					layer.close(appendindex);
				});
			}
		});
	})
}

function appendClose() {
	layer.close(appendindex);
}

function selectOnchang(obj) {
	var _this = $(obj)
	$("#model_id").val(_this.val());
}
function blurName(obj) {
	var _this = $(obj)
	$("#model_name").val(_this.val());
}
function blurKeyword(obj) {
	var _this = $(obj)
	$("#model_keyword").val(_this.val());
}
function selectType(obj) {
	var _this = $(obj)
	$("#model_type").val(_this.val());
}

function actionedit(id) {
	var datas = $("#model-action-infos").datagrid('getData');
	var item;
	for (var i = 0; i < datas.rows.length; i++) {
		if (id === datas.rows[i].id) {
			item = datas.rows[i];
		}
	}
	if (!item) {
		return;
	}
	layer.ready(function() {
		appendindex = layer.open({
			type : 1,
			title : '更新动作',
			skin : 'layui-layer-rim', // 加上边框
			area : [ '500px', '460px' ], // 宽高
			btn : [ '确定修改', '取消' ],
			content : html,
			success : function(layero, index) {
				$(".layui-layer-content input[name='modelName']").val(item.name);
				$(".layui-layer-content input[name='modelKeyword']").val(item.keyword);
				$(".layui-layer-content select[name='actionType']").val(item.type);
				$(".layui-layer-content select[name='modelId']").val(item.modelId);
				$(".layui-layer-content textarea[name='remark']").val(item.remark);
				$(".layui-layer-content input[name='modelOrder']").val(item.orderNum);
			},
			btn1 : function(index, layero) {
				if ($(".layui-layer-content .mloading").hasClass("active")) {
					return;
				}
				$(".layui-layer-content").mLoading("show");
				var modelId = $(".layui-layer-content select[name='modelId']").val();
				var name = $.trim($(".layui-layer-content input[name='modelName']").val());
				var keyword = $.trim($(".layui-layer-content input[name='modelKeyword']").val());
				var type = $(".layui-layer-content select[name='actionType']").val();
				var modelOrder = $(".layui-layer-content input[name='modelOrder']").val();
				if (isNaN(parseInt(modelOrder))) {
					modelOrder = 1;
				} else {
					modelOrder = parseInt(modelOrder);
				}
				var remark = $.trim($(".layui-layer-content textarea[name='remark']").val());
				if (name === '') {
					layer.msg('动作名称不能为空！', {
						icon : 2,
						time : 2000
					});
					return;
				}
				if (keyword === '') {
					layer.msg('动作关键字不能为空！', {
						icon : 2,
						time : 2000
					});
					return;
				}
				request.post('/backend/authorize/list/model/action/update', {
					id : id,
					modelId : modelId,
					name : name,
					keyword : keyword,
					type : type,
					orderNum : modelOrder,
					remark : remark
				}, function(response, data) {
					if (data && data.id && parseInt(data.id) > 0) {
						$('#model-action-infos').datagrid('reload');
					}
				}, null, function() {
					$(".layui-layer-content").mLoading("hide");
					layer.close(appendindex);
				});
			}
		})
	})
}

function actiondelet(id) {
	var datas = $("#model-action-infos").datagrid('getData');
	var item;
	for (var i = 0; i < datas.rows.length; i++) {
		if (id === datas.rows[i].id) {
			item = datas.rows[i];
		}
	}
	if (!item) {
		return;
	}
	layer.ready(function() {
		// 询问框
		layer.confirm('确认要删除？', {
			btn : [ '确认', '取消' ]
		// 按钮
		}, function(index) {
			request.post('/backend/authorize/list/model/action/delete', {
				id : item.id
			}, function(response, data) {
				if (data && data.id && parseInt(data.id) > 0) {
					$('#model-action-infos').datagrid('reload');
				}
			}, null, function() {
				layer.close(index);
			});
		});
	})
}
